package com.chcit.mobile.app.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.TextView;

import com.chcit.mobile.R;
import com.jess.arms.mvp.IPresenter;

import java.util.Objects;

public abstract class ScanFragment<P extends IPresenter> extends SupportToolbarFragement<P> {
    public static final String CODE_DATA ="SCAN_DATA";
    private static final String INTENT_ACTION_SCAN_RESULT="com.honeywell.spdbroadcast";
    protected LocalBroadcastManager broadcastManager;
    protected IntentFilter intentFilter;
    protected BroadcastReceiver mReceiver;
    private boolean onBroadFlag = true ;//广播开关
    protected boolean isShown = true;
    /**
     * 网络异常View
     */
    protected View errorView;
    protected TextView tvShowMsg;
    protected TextView tvTitle;



    protected abstract void onBarcodeEventSuccess(String barcodeData);
    protected void initBarcod(){
        broadcastManager = LocalBroadcastManager.getInstance(Objects.requireNonNull(getActivity()));
        intentFilter = new IntentFilter();
        intentFilter.addAction(INTENT_ACTION_SCAN_RESULT);
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){

                if (INTENT_ACTION_SCAN_RESULT.equals(intent.getAction())&&onBroadFlag) {
                    //获取扫描数据，并将扫描数据存放在barcodeData中
                    if (isShown) {
                        final String barcodeData = intent.getStringExtra(CODE_DATA);
                        onBarcodeEventSuccess(barcodeData);
                    }

                }


            }
        };
        broadcastManager.registerReceiver(mReceiver, intentFilter);
    }

    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        initBarcod();
        initEmityView();


    }
    public View getEmityViewParent(){
        return null;
    }

    private void initEmityView(){

        if(getEmityViewParent()!= null){
            errorView = getEmityViewParent().inflate(getContext(),R.layout.view_network_error, null);
            tvShowMsg = errorView.findViewById(R.id.tv_show_msg);
            tvTitle = errorView.findViewById(R.id.tv_show_title);
        }

    }
    @Override
    public void onPause() {
        super.onPause();
        onBroadFlag = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        onBroadFlag = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(broadcastManager != null){
            broadcastManager.unregisterReceiver(mReceiver);
        }
    }

   /* @Override
    public boolean onBackPressedSupport() {

        return true;

    }*/

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShown=!hidden;
    }

}
